﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.ComponentModel;
    using System.Threading;

    internal class EntityViewModel : INotifyPropertyChanged
    {
        private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextViewModel _contextViewModel;
        private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData _entityData;

        public event PropertyChangedEventHandler PropertyChanged;

        public EntityViewModel(Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextViewModel contextViewModel, Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData entityData)
        {
            this._contextViewModel = contextViewModel;
            this._entityData = entityData;
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            this.ContextViewModel.EntityStateChanged();
        }

        public override string ToString()
        {
            return this.Name;
        }

        public bool CanBeEdited
        {
            get
            {
                return this.EntityData.CanBeEdited;
            }
        }

        public bool CanBeIncluded
        {
            get
            {
                return this.EntityData.CanBeIncluded;
            }
        }

        public Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextViewModel ContextViewModel
        {
            get
            {
                return this._contextViewModel;
            }
        }

        public Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.EntityData EntityData
        {
            get
            {
                return this._entityData;
            }
        }

        public bool IsEditable
        {
            get
            {
                return (this.EntityData.IsEditable && this.EntityData.CanBeEdited);
            }
            set
            {
                if ((value != this.EntityData.IsEditable) && (!value || this.EntityData.CanBeEdited))
                {
                    this.EntityData.IsEditable = value;
                    this.RaisePropertyChanged("IsEditable");
                    if (value)
                    {
                        this.IsIncluded = true;
                    }
                }
            }
        }

        public bool IsIncluded
        {
            get
            {
                return (this.EntityData.IsIncluded && this.EntityData.CanBeIncluded);
            }
            set
            {
                if (this.EntityData.IsIncluded != value)
                {
                    this.EntityData.IsIncluded = value;
                    this.RaisePropertyChanged("IsIncluded");
                }
            }
        }

        public string Name
        {
            get
            {
                return this.EntityData.Name;
            }
        }
    }
}

